<?php

namespace app\common\model;

use think\Db;

/**
 *  内容contents表
 */
class Contents extends Base {

    /**
     * 获取列表
     */
    public function getAll() {
        return $this->order('id', 'desc')->select()->toArray();
    }
    /**
     * 获取列表分页
     */
    public function getPageDetile($where,$num)
    {
        return $this->where($where)->order('id', 'desc')->paginate($num);
    }
    /**
     * 前台获取列表
     */
    public function getFrontAll($where)
    {
        return $this->where($where)->order('id', 'asc')->select()->toArray();
    }
    /**
     * 获取采摘攻略列表图集
     */
    public function getGallery($id)
    {
        $s = $this->getById($id);
        $gallery = unserialize($s->gallery);
        if(empty($gallery))
        {
            $gallery = [$s->pic];
        }
        return $gallery;
    }
    
    /**
     * 获取列表
     */
    public function getlist($page, $size, $where = "", $order = 'id desc') {
        return $this->where($where)->order($order)->page($page, $size)->select()->toArray();
    }

    /**
     * 获取分类详情
     */
    public function getById($id) {
        return (Object) $this->where('id', $id)->find()->toArray();
    }

    /**
     * 获取josn用到的列表
     * $type 1查询分页 2查询总数
     */
    public function getListAll($type) {

        $page = input('get.page');
        $limit = input('get.limit');
        $where = '1=1';

        if (input('get.key/a')) {
            foreach (input('get.key/a') as $k => $v) {
                if (!empty($v)) {
                    switch ($k) {
                        case "title":
                            $where.=" and {$k} like '%{$v}%'";
                            break;
                        case "cid":
                            $where.=" and {$k} = '{$v}'";
                            break;
                        case "date":
                            $applydate = explode("--", $v);
                            $start = strtotime($applydate[0]);
                            $end = strtotime($applydate[1]);
                            $where .= " and '{$start}' <=  unix_timestamp(`time`) and '$end' >= unix_timestamp(`time`) ";
                            break;
                    }
                }
            }
        }
        if ($type == 1) {//分页
            return $this->where($where)->page($page, $limit)->select()->toArray();
        } else {//总数
            return $this->where($where)->select()->toArray();
        }
    }

}
